//
//  HCScoreStoreViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/2/8.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCScoreStoreViewController.h"

#import "HCHomeBottomView.h"

#import "HCGoodsListModel.h"

#import "HCCollectionViewCell.h"

@interface HCScoreStoreViewController () <HCHomeBottomViewSizeDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> {
    HCHomeBottomView *collectionView_;
    HCGoodsListModel *goodsModel;
}

@property (nonatomic, strong) UICollectionView *collectionView_;


@end

@implementation HCScoreStoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textTitle = @"积分商城";
    
//    collectionView_ = [[HCHomeBottomView alloc] initWithFrame:CGRectMake(0, 0, centerView.frame.size.width, centerView.frame.size.height)];
//    collectionView_.dataSource = self;
//    [centerView addSubview:collectionView_];
    
    
    //新商城
    
    //初始化视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, YBKAdaptiveH(16)*2.0)];
    view.backgroundColor = [UIColor whiteColor];
    [self.centerView addSubview:view];
    
    
//    self.backgroundColor = [UIColor whiteColor];
    
    UIImageView *topPrompt = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, YBKAdaptiveW(128), YBKAdaptiveH(16))];
    topPrompt.image = [UIImage imageNamed:@"商城"];
    [view addSubview:topPrompt];
    topPrompt.center = view.center;
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.minimumLineSpacing = 10;
    flow.minimumInteritemSpacing = 0;
    
    self.collectionView_ = [[UICollectionView alloc] initWithFrame:CGRectMake(0, view.frame.origin.y+view.frame.size.height, self.centerView.frame.size.width, self.centerView.frame.size.height - view.frame.size.height) collectionViewLayout:flow];
    self.collectionView_.delegate = self;
    self.collectionView_.dataSource = self;
    self.collectionView_.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    [self.centerView addSubview:self.collectionView_];
    
    [self.collectionView_ registerNib:[UINib nibWithNibName:@"HCCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ab"];
    
    
    self.dataRequest.delegate = self;
    [self.dataRequest postHC_Goods];
    
    // Do any additional setup after loading the view.
}

#pragma mark    UICollectionView代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (goodsModel) {
        return goodsModel.goods.count;
    }else{
        return 0;
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((YBKWinFrame.size.width-30)/2.0, YBKAdaptiveH(225));
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 10, 10);
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    HCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ab" forIndexPath:indexPath];
    
    if (goodsModel) {
        HCGoodsListData *data = goodsModel.goods[indexPath.row];
        cell.model = data;
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HCGoodsListData *data = goodsModel.goods[indexPath.row];
    
    [self pushChildrenViewController:@"HCGGoodsDetailViewController" parameterObject:data dataObject:data.url];
}



-(void)homeBottomViewSize:(CGFloat)height bottomView:(HCHomeBottomView *)homeBottomView {
    homeBottomView.frame = CGRectMake(0, 0, self.centerView.frame.size.width, self.centerView.frame.size.height - YBKAdaptiveH(16));
//    homeBottomView.collectionView_.frame = CGRectMake(0, YBKAdaptiveH(16)*2, centerView.frame.size.width, centerView.frame.size.height - YBKAdaptiveH(16)*2);
    homeBottomView.collectionScolled = YES;
}

#pragma mark 商品列表点击
-(void)goodsViewClicked:(NSIndexPath *)indexPath {
    NSLog(@"商品点击了");
    
    HCGoodsListData *data = goodsModel.goods[indexPath.row];
    
    [self pushChildrenViewController:@"HCGGoodsDetailViewController" parameterObject:data dataObject:data.url];
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"积分商城失败");
}

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    NSLog(@"积分商城成功:%@",dictionary);
    goodsModel = [[HCGoodsListModel alloc] initWithDictionary:dictionary error:nil];
//    collectionView_.model_goods = goodsModel;
    [self.collectionView_ reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
